home *** CD-ROM | disk | FTP | other *** search
- Path: comma.rhein.de!yaps!arno
- From: arno@yaps.rhein.de (Arno Eigenwillig)
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: File sizes
- Message-ID: <zrNcy*T5g@yaps.rhein.de>
- Date: Thu, 07 Mar 1996 21:32:19 +0100
- References: <4gd9pu$cpr@mn5.swip.net> <WXQ7y*5+f@yaps.rhein.de> <4gjs20$i68@btmpjg.god.bel.alcatel.be>
- <W7s9y*i1g@yaps.rhein.de> <4h66o8$egk@btmpjg.god.bel.alcatel.be>
- Organization: Yet Another Private Site in Meckenheim, Germany
- X-Copyright: This article may not be distributed on a CD-ROM
- or in printed form without prior written consent of the author.
- X-Newsreader: Arn V 1.04
-
- In article <4h66o8$egk@btmpjg.god.bel.alcatel.be>, writes:
-
- > ..tell me: what is the correct ANSI-way of getting the correct size
- > of a file, even files that are not opened in binary mode?
-
- unsigned long c = ~0; /* indicates error */
-
- /* If f is a text or binary file, pipe, console etc.: */
- c = 0;
- while (fgetc(f) != EOF) {
- c++;
- }
-
- /* If f is a binary real file: */
- if (fseek(f, 0, SEEK_END) != -1) {
- c = ftell(f);
- }
-
- /* May or may not work: */
- fseek(f, 0, SEEK_SET); /* -1 on error */
-
- The problem is that ANSI-C file I/O does not differenciate between
- real files and other stuff and that fseek()/ftell() usually will
- operate with OS-level offsets into the file, but what calls to fgetc()
- return may well be different from what the file looks like at OS
- level: The file I/O functions might have to translate end-of-line
- sequences to '\n' and block-oriented files into streams. Therefore,
- there is no 1:1 correspondance of ftell() results and a number of
- chars read or written.
-
- A function like fstat() does just not exist on ANSI level.
-
- -- __
- __/// Arno Eigenwillig /\ <arno@yaps.rhein.de> \/ PGP key available.
- \XX/ V+49-2225-5870 /\ <Arnooo @ #amigager> \/ MIME 8bit welcome.
-